package org.dzms.commons;

import java.util.ArrayList;
import java.util.List;

public class PageHandler {
	private IPage page;
	public PageHandler(IPage dao) {
		this.page = dao;
	}
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCountPage() {
		return countPage;
	}

	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}

	public int getPrev() {
		return prev;
	}

	public void setPrev(int prev) {
		this.prev = prev;
	}

	public int getNext() {
		return next;
	}

	public void setNext(int next) {
		this.next = next;
	}

	public List getData() {
		return data;
	}

	public void setData(List data) {
		this.data = data;
	}

	public String getCond() {
		return cond;
	}

	public void setCond(String cond) {
		this.cond = cond;
	}
	private int currentPage = 1;
	private int pageSize = 15;
	private int countPage = 0;
	private int prev = 0;
	private int next = 0;
	private List data = new ArrayList();
	private String cond = "";
	
	public PageHandler findBean() {
		countPage = page.findCountPage(this);
		prev = (currentPage-1) <= 1?1:(currentPage-1);
		next = (currentPage+1) >= countPage?countPage:(currentPage+1);
		data = page.findCurrentPageData(this);
		return this;
	}
	public PageHandler findBean1() {
		countPage = page.findCountPage(this);
		prev = (currentPage-1) <= 1?1:(currentPage-1);
		next = (currentPage+1) >= countPage?countPage:(currentPage+1);
		data = page.findCurrentPageDatadesc(this);
		return this;
	}
	public PageHandler findBean2(String a) {
		countPage = page.findCountPage(this);
		prev = (currentPage-1) <= 1?1:(currentPage-1);
		next = (currentPage+1) >= countPage?countPage:(currentPage+1);
		data = page.foods(a,this);
		return this;
	}
	//语句进入："列名=值，列名#=值，列名=x-y，列名>值"
	//等于直接等于某值或者希望模糊值等于某值，再或者等于一个范围
	public String getConditonSQL() {
		String sql = " ";
		if (cond != null) {
			//分隔语句
			String[] conds = cond.split(",");
			for (String cd : conds) {
				String cds=cd.replaceAll("^|&", "");
				//查找条件中是否含有^和&
				if(cds.indexOf("=")!=-1){
					if (cds.indexOf("#=") != -1) {
						String[] kv = cds.split("#=");
						sql += "" + kv[0] + "like '%" + kv[1] + "%' ";
					}else if (cds.indexOf("-") != -1) {
						sql += "" + cds.replace("=", " between ").replace("-", " and ");
					}else {
						sql += "" + cds;
					}
					sql += " and";
				}
			}
			sql = sql.replaceFirst("(and)$", "");
			for(String cd:conds){
				if(sql.indexOf("order by")==-1){
					sql+=" order by ";
				}
				if(cd.indexOf("^")!=-1){
					String [] condtions = cd.split("\\#\\=|=");
					
					sql+= " "+condtions[0].replace("^", "") +" asc,";
				}else if(cd.indexOf("&")!=-1){
					String [] condtions = cd.split("\\#\\=|=");
					sql+=" "+condtions[0].replace("&", "")+" desc,";
				}
			}
			sql = sql.replaceFirst(",$", "");
		}
		
		return sql;
	}
	public static void main(String[] args) {
		PageHandler ph = new PageHandler(null);
		ph.setCond("username#=ab, password=123, username^");
		System.out.println(ph.getConditonSQL());
	}
}
